<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '../api/auth.js'
import { BASE_URL } from '../api/config.js'

const router = useRouter()
const loginForm = reactive({
  username: '',
  password: ''
})
const loading = ref(false)
const errorMessage = ref('')
const apiUrl = ref(BASE_URL)

const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    errorMessage.value = '请输入用户名和密码'
    return
  }
  loading.value = true
  errorMessage.value = ''
  try {
    const result = await login(loginForm.username, loginForm.password)
    if (result.success) {
      localStorage.setItem('username', loginForm.username)
      localStorage.setItem('token', result.token)
      ElMessage.success('登录成功')
      router.push('/admin')
    } else {
      errorMessage.value = result.message || '登录失败'
      ElMessage.error(errorMessage.value)
    }
  } catch (error) {
    errorMessage.value = '登录失败，请重试'
    ElMessage.error(errorMessage.value)
  } finally {
    loading.value = false
  }
}

// 回车键登录
const handleKeyPress = (event) => {
  if (event.key === 'Enter') {
    handleLogin()
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="login-title">小说网站管理系统</h1>
        <p class="login-subtitle">Novel Management System</p>
      </div>
      
      <el-form :model="loginForm" class="login-form" @submit.prevent="handleLogin">
        <el-form-item label="用户名">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" :disabled="loading" @keyup.enter="handleLogin" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" :disabled="loading" @keyup.enter="handleLogin" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" style="width:100%" @click="handleLogin">登录</el-button>
        </el-form-item>
        <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
      </el-form>

      <div class="login-footer">
        <p class="demo-info">API地址：{{ apiUrl }}</p>
        <p class="copyright">© 2024 小说网站管理系统</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.login-container {
  min-height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  margin: 0;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.login-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #2d3748;
  margin-bottom: 8px;
}

.login-subtitle {
  font-size: 14px;
  color: #718096;
  font-weight: 500;
}

.login-form {
  margin-bottom: 30px;
}

.error-message {
  background: #fed7d7;
  color: #c53030;
  padding: 12px;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 20px;
  border: 1px solid #feb2b2;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e2e8f0;
}

.demo-info {
  font-size: 12px;
  color: #718096;
  margin-bottom: 8px;
  background: #f7fafc;
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid #e2e8f0;
}

.copyright {
  font-size: 12px;
  color: #a0aec0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 10px;
  }
  
  .login-card {
    padding: 30px 20px;
    margin: 0;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .form-input {
    font-size: 16px; /* 防止iOS缩放 */
  }
}
</style> 